package com.kin207.netty.server;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerManager {
	final static Logger logger = LoggerFactory.getLogger(ServerManager.class);
	
	protected Map<String,IServer> servers = new ConcurrentHashMap();
	
	private static ServerManager instance;
	
	private static ServerManager shared;
	
	public ServerManager(){
		shared = this;
	}
	
	public static synchronized ServerManager newInstance(){
		if(instance==null){
			instance = new ServerManager();
		}
		return instance;
	}
	
	public void addServer(String name, IServer server){
		logger.info("add server, name:{}, server:{}", name, server);
		servers.put(name, server);
	}
	
	public void addServer(IServer server){
		addServer(server.getName(), server);
	}
	
	public IServer removeServer(String name){
		IServer server = servers.remove(name);
		logger.info("remove server, name:{}, server:{}", name, server);
		return server;
	}
	
	public IServer getServer(String name){
		return servers.get(name);
	}
	
	public int servers(){
		return servers.size();
	}
	
	public boolean exists(String name){
		return servers.containsKey(name);
	}

	public Map<String, IServer> getServers() {
		return servers;
	}

	public void setServers(Map<String, IServer> servers) {
		this.servers = servers;
	}

	public static ServerManager getShared() {
		return shared;
	}

	public static void setShared(ServerManager shared) {
		ServerManager.shared = shared;
	}
	
	
}
